﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AppManager.Background
{
    /// <summary>
    /// 抛出各种异常来测试配置是否起作用
    /// </summary>
    public class TestExceptionController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 抛出HTTP 500
        /// </summary>
        /// <returns></returns>        
        public ActionResult ThrowHttp500()
        {
            throw new HttpException(500, "服务器错误");
        }
        /// <summary>
        /// 抛出HTTP 404
        /// </summary>
        /// <returns></returns>
        public ActionResult ThrowHttp404()
        {
            throw new HttpException(404, "页面未找到");
        }

        /// <summary>
        /// 抛出未引用对象异常
        /// ，此处单独使用HandleError特性
        /// ，并指定异常类型及响应视图
        /// </summary>
        /// <returns></returns>
        [HandleError(ExceptionType = typeof(NullReferenceException), View = "CustomError")]
        public ActionResult ThrowNullReferenceException()
        {
            throw new NullReferenceException();
        }
        /// <summary>
        /// 引发输入字符串的格式不正确异常
        /// ，此处指定了响应的错误页面
        /// ，由于是不同的控制器所以要完整的相对路径
        /// </summary>
        /// <returns></returns>
        [HandleError(View = "~/Views/Error/CustomHttpError.cshtml")]
        public ActionResult ThrowFormatException()
        {
            string str = "";
            int count = Convert.ToInt32(str);
            return View("Index");
        }
    }
}